package com.longcheer.weibo;

import com.longcheer.weibo.api.StatusesApi;
import com.longcheer.weibo.util.Utils;
import com.weibo.net.WeiboException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.util.Log;
import android.view.View;

public class PublishActivity extends Activity implements View.OnClickListener {
    
    private static final String TAG = "PublishActivity";
    
    private static final int MSG_PUBLISH_BEGIN = 0;
    private static final int MSG_PUBLISH_END = 1;
    private static final int MSG_PUBLISH_ERR = 3;
    
    private EditText mWeiboBody;
    private Button mPublish;
    
    private ProgressDialog mProgress;
    
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_PUBLISH_BEGIN:
                String weiboText = msg.getData().getString("WeiboText");
                if (publishWeibo(weiboText)) {
                    mHandler.sendEmptyMessage(MSG_PUBLISH_END);
                } else {
                    mHandler.sendEmptyMessage(MSG_PUBLISH_ERR);
                }
                break;
            case MSG_PUBLISH_END:
                mProgress.dismiss();
                Toast.makeText(PublishActivity.this, R.string.publish_ok, Toast.LENGTH_SHORT);
                finish();
                break;
            case MSG_PUBLISH_ERR:
                mProgress.dismiss();
                Toast.makeText(PublishActivity.this, R.string.publish_error, Toast.LENGTH_SHORT);
                break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.publish);
        
        mWeiboBody = (EditText)findViewById(R.id.et_weibo_text);
        mPublish = (Button)findViewById(R.id.btn_publish);
        
        mPublish.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == mPublish) {
            String weiboText = mWeiboBody.getText().toString();
            if (Utils.isEmpty(weiboText)) {
                showAlertDialog(R.string.weibo_cannot_empty);
            } else {
                Message msg = mHandler.obtainMessage(MSG_PUBLISH_BEGIN);
                Bundle data = new Bundle();
                data.putString("WeiboText", weiboText);
                msg.setData(data);
                mHandler.sendMessage(msg);
                
                String title = getResources().getString(R.string.app_name);
                String message = getResources().getString(R.string.publishing);
                mProgress = ProgressDialog.show(this, title, message);
            }
        }
    }
    
    private boolean publishWeibo(String text) {
        boolean ok = true;
        try {
            StatusesApi.updateWeibo(this, text, StatusesApi.INVALID_LONG);
        } catch (WeiboException e) {
            Log.e(TAG, "publish weibo error.", e);
            ok = false;
        }
        
        return ok;
    }
    
    private void showAlertDialog(int textId) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.app_name);
        builder.setMessage(textId);
        builder.setPositiveButton(R.string.ok, new android.content.DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
    }
}
